package com.rykj.common.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rykj.common.R;
import com.rykj.common.base.BaseView;
import com.rykj.common.base.EmptyEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.rykj.common.utils.HideNavBarUtil;
import com.rykj.common.utils.Screen;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

/**
 * Created by xianrui on 2019-08-29.
 *
 * @author xianrui
 */
public abstract class BaseActivity extends AppCompatActivity implements BaseView {

    public static Boolean isAllFullScreen = false;
    public static Boolean isCancelBackKey = false;

    protected boolean isFullScreen() {
        return isAllFullScreen;
    }

    protected Context getContext() {
        return this;
    }

    protected Activity getActivity() {
        return this;
    }

    protected abstract int getLayoutId();

    ViewGroup mRootLayout;
    View mStatusBarView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        if (isFullScreen()) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            HideNavBarUtil.hideBottomUIMenu(getWindow().getDecorView());
        }

        initStatusBar();

        init(savedInstanceState);


    }

    private void initStatusBar() {
        if (getLayoutId() > 0) {
            if (isFullScreen() || !isPaddingStatusBar()) {
                setContentView(getLayoutId());
            } else {
                mRootLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.root_layout, null, false);
                mStatusBarView = mRootLayout.getChildAt(0);
                if (getStatusBarColor() != 0) {
                    mStatusBarView.setBackgroundColor(getStatusBarColor());
                }
                ViewGroup.LayoutParams statusBarLayoutParams = mStatusBarView.getLayoutParams();
                statusBarLayoutParams.height = Screen.getStatusBarHeight();
                mStatusBarView.setLayoutParams(statusBarLayoutParams);
                LayoutInflater.from(this).inflate(getLayoutId(), mRootLayout, true);
                setContentView(mRootLayout);
            }
            ButterKnife.bind(this, this);
        }
    }

    protected boolean isPaddingStatusBar() {
        return false;
    }

    protected int getStatusBarColor() {
        return 0;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEmptyEvent(EmptyEvent emptyEvent) {

    }

    protected abstract void init(Bundle savedInstanceState);

    protected void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        if (isCancelBackKey) {
            return;
        } else {
            super.onBackPressed();
        }
    }
}
